package com.atguigu.eduService.controller;

import com.atguigu.commonutils.R;
import com.atguigu.eduService.entity.EduCourse;
import com.atguigu.eduService.entity.EduTeacher;
import com.atguigu.eduService.entity.vo.CourseInfoVo;
import com.atguigu.eduService.entity.vo.CoursePublishVo;
import com.atguigu.eduService.entity.vo.TeacherQuery;
import com.atguigu.eduService.service.EduCourseService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author 亚当
 * @since 2022-06-24
 */
@RestController
@RequestMapping("/eduService/course")
@CrossOrigin
public class EduCourseController {
    @Autowired
    private EduCourseService courseService;

    //根据课程id删除课程所有数据
    @ApiOperation("根据课程id删除课程所有数据")
    @GetMapping("deleteCourseById/{courseId}")
    public R deleteCourseById(@PathVariable String courseId){
        boolean flag = courseService.deleteCourseById(courseId);
        //boolean flag = courseService.removeById(courseId);
        if(flag){
            return R.ok();
        }else {
            return R.error();
        }
    }
    //分页查询课程列表
    @ApiOperation(value = "分页查询课程")
    @GetMapping("getCourseListPage/{current}/{limit}")
    public R getCourseListPage(@PathVariable long current,
                               @PathVariable long limit){
        Page<EduCourse> eduCoursePage = new Page<>(current,limit);
        courseService.page(eduCoursePage, null);
        long total = eduCoursePage.getTotal(); //总记录数
        List<EduCourse> records = eduCoursePage.getRecords(); // 数据List集合
        HashMap map = new HashMap<>();
        map.put("total",total);
        map.put("rows",records);
        return R.ok().data(map);
    }
    // 条件查询带分页的方法
    @ApiOperation(value = "条件查询带分页的方法")
    @PostMapping("pageCourseCondition/{current}/{limit}")
    public R pageCourseCondition(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) EduCourse eduCourse){
        // 创建page对象
        Page<EduCourse> eduCoursePage = new Page<>(current,limit);
        /*try{
            int i=10/0;
        }catch (Exception e){
            throw new GuliException(20001,"执行了自定义异常...");
        }*/
        // 构建条件
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        // 判断条件是否为空，如果不为空则拼接条件
        String title = eduCourse.getTitle();
        String status = eduCourse.getStatus();
        Integer lessonNum = eduCourse.getLessonNum();
        Date gmtCreate = eduCourse.getGmtCreate();
        Date gmtModified = eduCourse.getGmtModified();
        Long viewCount = eduCourse.getViewCount();

        if(!StringUtils.isEmpty(title)){
            queryWrapper.like("title",title) ;
        }
        if(!StringUtils.isEmpty(status)){
            queryWrapper.eq("status",status);
        }
        if(!StringUtils.isEmpty(gmtCreate)){
            queryWrapper.ge("gmt_create",gmtCreate);
        }
        if(!StringUtils.isEmpty(gmtModified)){
            queryWrapper.le("gmt_modified",gmtModified);
        }
        if(!StringUtils.isEmpty(lessonNum)){
            queryWrapper.eq("lesson_num",lessonNum);
        }
        if(!StringUtils.isEmpty(viewCount)){
            queryWrapper.eq("view_count",viewCount);
        }
        //排序
        queryWrapper.orderByDesc("gmt_create");
        // 调用方法实现条件查询分页
        IPage<EduCourse> page = courseService.page(eduCoursePage, queryWrapper);
        long total = eduCoursePage.getTotal();//总记录数
        List<EduCourse> records = eduCoursePage.getRecords();// 数据List集合
        return R.ok().data("items",records).data("total",total);
    }
    //课程列表
    @GetMapping("getCourseList")
    public R getCourseList(){
        List<EduCourse> list = courseService.list(null);
        return R.ok().data("list",list);
    }

    //添加课程基本信息的方法
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        String id = courseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId",id);
    }
    //根据课程id查询课程基本信息的方法
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
        CourseInfoVo courseInfoVo = courseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo",courseInfoVo);
    }
    //修改课程信息
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        courseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }
    //根据课程id查询课程确认信息
    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id){
        CoursePublishVo coursePublishVo = courseService.publishCourseInfo(id);
        return R.ok().data("coursePublishVo",coursePublishVo);
    }
    //课程最终发布
    //修改课程状态
    @PostMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable String id){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        boolean flag = courseService.updateById(eduCourse);
        if(!flag){
            throw new GuliException(20001,"课程发布异常");
        }
        return R.ok();
    }
}

